package com.software.backend.center.controller;

import cn.hutool.http.HttpResponse;
import com.google.gson.Gson;
import com.software.backend.center.service.UserExtendsService;
import com.software.backend.login.entity.UserExtends;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.*;
import java.net.URI;

/**
 * @program: back-end
 * @description:
 * @author: C@t
 * @create: 2022-12-24 10:51
 **/
@RestController
@RequestMapping("center")
public class UserExtendsController {
    @Autowired
    private UserExtendsService parentExtendsService;

    Gson gson = new Gson();
    @PostMapping("/findUserExtends")
    public UserExtends findById(@RequestParam Long id ) {
        UserExtends userExtends = parentExtendsService.findById(id);
        return userExtends;
    }
    @PostMapping("/updateUserExtends")
    public UserExtends updateUserExtends(@RequestParam String userExtends){
        UserExtends userExtends1 = gson.fromJson(userExtends, UserExtends.class);
        UserExtends updateUserExtends = parentExtendsService.updateUserExtends(userExtends1);
        return updateUserExtends;
    }
    @GetMapping("/getHead")
    public void getHead(@RequestParam Long id, HttpServletResponse response) throws IOException {
        String avatar = parentExtendsService.getAvatarUrlById(id);
        System.out.println(avatar);
        File file = new File(avatar);
        FileInputStream fis = new FileInputStream(file);
        byte[] b = new byte[fis.available()];
        fis.read(b);
        ServletOutputStream out = response.getOutputStream();
        out.write(b);
        out.flush();
    }

    @PostMapping("/updateHead")
    public void updateHead(@RequestParam Long id, HttpServletResponse response) throws IOException {
        String avatar = parentExtendsService.getAvatarUrlById(id);
        File file = new File(avatar);
        FileInputStream fis = new FileInputStream(file);
        byte[] b = new byte[fis.available()];
        fis.read(b);
        ServletOutputStream out = response.getOutputStream();
        out.write(b);
        out.flush();
    }
}
